<template>
    <div class="contener">
        <!-- 页面 -->
        <div class="page">
            <!-- 搜索 -->
            <transition
                @before-enter="_beforeEnter"
                @enter="_enter"
                @after-enter="_afterEnter"
                @before-leave="_beforeLeave"
                @leave="_leave"
                @after-leave="_afterLeave"
            >
                <div
                    class="search-contener"
                    ref="searchContener"
                    v-show="searchFormConfig.searchContenerShow"
                >
                    <base-form
                        :formItems="searchFormConfig.formItems"
                        :formData="searchFormConfig.formSubmit"
                        ref="searchForm"
                        @onButtonClick="searchBarButtonClick"
                    />
                </div>
            </transition>
            <!-- 表格 -->
            <div class="datas-contener">
                <!-- 表格操作项 -->
                <div class="option-contener">
                    <data-options
                        :leftConfig="buttonConfig.leftConfig"
                        @buttonClick="handelOptionClick"
                    />
                </div>
                <!-- 表格展示项 -->
                <div class="table-contener">
                    <base-table
                        ref="baseTable"
                        :tableData="baseTableConfig.tableData"
                        :tableHeader="baseTableConfig.tableHeader"
                        :tableConfig="baseTableConfig.tableConfig"
                        @handleSelection="tableDataSelected"
                    >
                        <template slot-scope="{ scope }">
                            <span v-if="scope.type === 'index'">
                                {{ scope.$index + 1 }}
                            </span>
                            <el-tag
                                v-if="scope.type === 'tag'"
                                :type="
                                    scope.row.master == true
                                        ? 'danger'
                                        : 'primary'
                                "
                                size="small"
                                >{{
                                    scope.row.master == true ? '是' : '否'
                                }}</el-tag
                            >
                            <el-tag
                                v-if="scope.type === 'storageName'"
                                :type="'primary'"
                                >{{
                                    scope.row.storage.toString()
                                        | getAttrName(
                                            storageList,
                                            'value',
                                            'label'
                                        )
                                }}</el-tag
                            >

                            <div v-if="scope.type === 'handle'">
                                <el-button
                                    type="text"
                                    @click.stop="updateTableData(scope.row)"
                                    >修改</el-button
                                >
                                <el-button
                                    type="text"
                                    @click.stop="setMasterTableData(scope.row)"
                                    >主配置</el-button
                                >
                                <el-button
                                    type="text"
                                    @click.stop="
                                        testFileConfigUpload(scope.row)
                                    "
                                    >测试</el-button
                                >
                                <el-button
                                    type="text"
                                    @click.stop="deleteTableData(scope.row)"
                                    >删除</el-button
                                >
                            </div>
                        </template>
                    </base-table>
                </div>
                <!-- 分页 -->
                <div class="paging-contener">
                    <pagin-ation
                        :size="paginationConfig.size"
                        :total="paginationConfig.total"
                        :type="paginationConfig.type"
                        :currentPage="paginationConfig.currentPage"
                        @PageNumber="pageNumberChange"
                        @PageSize="pageSizeChange"
                    />
                </div>
            </div>
        </div>
        <!-- 弹框 -->
        <el-dialog
            :title="dialogConfig.title"
            :visible.sync="dialogConfig.show"
            :width="dialogConfig.width"
            v-dialogDrag
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
        >
            <!-- 筛选表格列 -->
            <div v-if="dialogConfig.key === 'tableColumn'">
                <base-transfer
                    :transferConfig="tableColumnTransferConfig"
                    ref="baseTransfer"
                />
            </div>
            <!-- 新增 -->
            <div v-if="dialogConfig.key === 'addTableData'">
                <base-form
                    :formItems="addFormConfig.formItems"
                    :formData="addFormConfig.formSubmit"
                    :formConfig="addFormConfig.formConfig"
                    :formRules="addFormConfig.formRule"
                    ref="addForm"
                    @onButtonClick="addFormButtonClick"
                    @onSelectChange="handerOnSelectChange"
                />
            </div>
            <!-- 修改 -->
            <div v-if="dialogConfig.key === 'updateTableData'">
                <base-form
                    :formItems="updateFormConfig.formItems"
                    :formData="updateFormConfig.formSubmit"
                    :formConfig="updateFormConfig.formConfig"
                    :formRules="updateFormConfig.formRule"
                    ref="updateForm"
                    @onButtonClick="updateFormButtonClick"
                />
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="handelCloseDialog"
                    >取 消</el-button
                >
                <el-button
                    size="small"
                    type="primary"
                    @click="handelCofimeDialog"
                    >确 定</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>
<script>
import BaseForm from '@/components/baseForm/index'
import DataOptions from '@/components/dataOptions/index'
import BaseTable from '@/components/baseTable/index'
import PaginAtion from '@/components/pagination/index'
import baseTransfer from '@/components/baseTransfer/index'
import globalMixin from '@/mixins/global'
import {
    listFileConfig,
    saveFileConfig,
    updateFileConfig,
    updateFileConfigMaster,
    deleteFileConfig,
    testFileConfig
} from '@/http/apis/tool/file/index'
import { getDictDatas } from '@/plugins/dict'
import { assemblyDataByTableColumn, setFormItemProp } from '@/utils/tool'
import { mapState } from 'vuex'
export default {
    components: {
        BaseForm,
        DataOptions,
        BaseTable,
        PaginAtion,
        baseTransfer
    },
    computed: {
        ...mapState({
            postInfo: state => state.post.postInfo
        })
    },
    mixins: [globalMixin],
    dicts: ['sys_file_storage'],
    data() {
        return {
            // 通用树组件配置
            baseTreeConfig: {
                // tree的数据
                data: [],
                // tree的加载状态
                loading: false
            },
            storageList: getDictDatas('sys_file_storage'),
            buttonConfig: {
                leftConfig: [
                    {
                        prop: 'addTableData',
                        label: '新增',
                        icon: 'el-icon-plus',
                        type: 'primary',
                        disabled: false,
                        hasPermi: ['/tool/file/config/save']
                    }
                ]
            },
            // 搜索表单配置
            searchFormConfig: {
                formItems: [
                    [
                        {
                            ltype: 'input', // 大类型（input）
                            placeholder: '请输入配置名', // 文字提示
                            label: '配置名',
                            prop: 'name', // 绑定的key
                            width: 'w-auto'
                        },
                        {
                            ltype: 'select', // 大类型（input）
                            placeholder: '请选择存储器', // 文字提示
                            label: '存储器',
                            prop: 'storage', // 绑定的key
                            width: 'w-auto',
                            options: getDictDatas('sys_file_storage')
                        },
                        {
                            ltype: 'button', // 大类型（input）
                            type: 'primary',
                            text: '搜索', // 文字提示
                            prop: 'searchDatas', // 绑定的key
                            icon: 'el-icon-search',
                            width: 'w-auto',
                            loading: false
                        },
                        {
                            ltype: 'button', // 大类型（input）
                            type: 'default',
                            text: '重置', // 文字提示
                            prop: 'resetSearchForm', // 绑定的key
                            icon: 'el-icon-refresh-right',
                            width: 'w-auto'
                        }
                    ]
                ],
                formSubmit: {
                    code: '',
                    name: '',
                    status: ''
                },
                formConfig: {
                    labelPosition: 'right', // label对齐方式
                    labelWidth: 'auto'
                },
                // 搜索控件是否显示
                searchContenerShow: true
            },
            // 表格配置
            baseTableConfig: {
                // 表格数据
                tableData: [],
                // 表格头数据
                tableHeader: [
                    {
                        type: 'index',
                        label: '序号',
                        width: 50,
                        visible: true
                    },
                    {
                        prop: 'name',
                        label: '配置名',
                        visible: true
                    },
                    {
                        label: '存储器',
                        visible: true,
                        type: 'storageName'
                    },
                    {
                        prop: 'master',
                        label: '主配置',
                        type: 'tag',
                        visible: true
                    },
                    {
                        prop: 'createTime',
                        label: '创建时间',
                        visible: true
                    },
                    {
                        prop: 'remark',
                        label: '备注',
                        visible: true
                    },
                    {
                        label: '操作',
                        type: 'handle',
                        fixed: 'right',
                        visible: true
                    }
                ],
                // 表格配置
                tableConfig: {
                    loading: false
                },
                // 选中的表格数据
                tableCheckedDatas: []
            },
            // 分页配置
            paginationConfig: {
                type: 'md',
                size: 10,
                currentPage: 1,
                total: 100
            },
            // 弹框配置
            dialogConfig: {
                // 弹框标题
                title: '新增',
                // 是否显示
                show: false,
                // 宽度
                width: '30%',
                // 弹框标识(用来区分弹框内容)
                key: ''
            },
            // 查询表格数据的参数
            queryTableParams: {
                pageNo: 0,
                pageSize: 10,
                entity: {}
            },
            // 表格列筛选配置
            tableColumnTransferConfig: {
                // 穿梭框的数据
                data: [],
                // 穿梭框的标题
                titles: ['显示', '隐藏'],
                // 隐藏的列
                modelList: []
            },
            // 新增表单配置
            addFormConfig: {
                // 表单项
                formItems: [
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入配置名',
                            label: '配置名',
                            prop: 'name',
                            width: 'w-1'
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            type: 'textarea',
                            placeholder: '请输入备注',
                            label: '备注',
                            prop: 'remark',
                            width: 'w-1'
                        }
                    ],

                    [
                        {
                            ltype: 'select',
                            placeholder: '请选择存储器',
                            label: '存储器',
                            prop: 'storage',
                            width: 'w-1',
                            options: getDictDatas('sys_file_storage')
                        }
                    ],
                    //数据库的
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入自定义域名',
                            label: '自定义域名',
                            prop: 'domain',
                            width: 'w-1',
                            isShow: false,
                            question:
                                '文件服务器所在的地址，比如：http://192.168.50.13:9000，如果配置了域名，则使用域名'
                        }
                    ],
                    //本地磁盘
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入基础路径',
                            label: '基础路径',
                            prop: 'basePath',
                            width: 'w-1',
                            isShow: false
                        }
                    ],

                    //FTP //SFTP
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入主机地址',
                            label: '主机地址',
                            prop: 'host',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'inputNumber',
                            placeholder: '请输入主机端口',
                            label: '主机端口',
                            prop: 'port',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入用户名',
                            label: '用户名',
                            prop: 'username',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入密码',
                            label: '密码',
                            prop: 'password',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    //FTP
                    [
                        {
                            ltype: 'radio',
                            placeholder: '请选择连接模式',
                            label: '连接模式',
                            prop: 'mode',
                            width: 'w-1',
                            isShow: false,
                            options: getDictDatas('ftp_connect_type')
                        }
                    ],
                    //S3对象存储
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入节点地址',
                            label: '节点地址',
                            prop: 'endpoint',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入存储 Bucket',
                            label: '存储 Bucket',
                            prop: 'bucket',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入accessKey',
                            label: 'accessKey',
                            prop: 'accessKey',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入accessSecret',
                            label: 'accessSecret',
                            prop: 'accessSecret',
                            width: 'w-1',
                            isShow: false
                        }
                    ]
                ],
                // 提交项
                formSubmit: {
                    mode: '1',
                    remark: ''
                },
                // 配置项
                formConfig: {
                    labelPosition: 'right', // label对齐方式
                    labelWidth: '120px'
                },
                // 验证项
                formRule: {
                    name: [
                        {
                            required: true,
                            message: '请输入配置名',
                            trigger: 'blur'
                        }
                    ],
                    storage: [
                        {
                            required: true,
                            message: '请选择存储器',
                            trigger: 'blur'
                        }
                    ],
                    domain: [
                        {
                            required: false,
                            message: '请输入自定义域名',
                            trigger: 'blur'
                        }
                    ],
                    basePath: [
                        {
                            required: false,
                            message: '请输入基础路径',
                            trigger: 'blur'
                        }
                    ],
                    host: [
                        {
                            required: false,
                            message: '请输入主机地址',
                            trigger: 'blur'
                        }
                    ],
                    port: [
                        {
                            required: false,
                            message: '请输入主机端口',
                            trigger: 'blur'
                        }
                    ],
                    username: [
                        {
                            required: false,
                            message: '请输入用户名',
                            trigger: 'blur'
                        }
                    ],
                    password: [
                        {
                            required: false,
                            message: '请输入密码',
                            trigger: 'blur'
                        }
                    ],
                    mode: [
                        {
                            required: false,
                            message: '请选择连接模式',
                            trigger: 'blur'
                        }
                    ],
                    endpoint: [
                        {
                            required: false,
                            message: '请输入节点地址',
                            trigger: 'blur'
                        }
                    ],
                    bucket: [
                        {
                            required: false,
                            message: '请输入存储 Bucket',
                            trigger: 'blur'
                        }
                    ],
                    accessKey: [
                        {
                            required: false,
                            message: '请输入accessKey',
                            trigger: 'blur'
                        }
                    ],
                    accessSecret: [
                        {
                            required: false,
                            message: '请输入accessSecret',
                            trigger: 'blur'
                        }
                    ]
                }
            },
            // 修改表单配置
            updateFormConfig: {
                // 表单项
                formItems: [
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入配置名',
                            label: '配置名',
                            prop: 'name',
                            width: 'w-1'
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            type: 'textarea',
                            placeholder: '请输入备注',
                            label: '备注',
                            prop: 'remark',
                            width: 'w-1'
                        }
                    ],

                    [
                        {
                            ltype: 'select',
                            placeholder: '请选择存储器',
                            label: '存储器',
                            prop: 'storage',
                            width: 'w-1',
                            options: getDictDatas('sys_file_storage'),
                            disabled: false
                        }
                    ],
                    //数据库的
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入自定义域名',
                            label: '自定义域名',
                            prop: 'domain',
                            width: 'w-1',
                            isShow: false,
                            question:
                                '文件服务器所在的地址，比如：http://192.168.50.13:9000，如果配置了域名，则使用域名'
                        }
                    ],
                    //本地磁盘
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入基础路径',
                            label: '基础路径',
                            prop: 'basePath',
                            width: 'w-1',
                            isShow: false
                        }
                    ],

                    //FTP //SFTP
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入主机地址',
                            label: '主机地址',
                            prop: 'host',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'inputNumber',
                            placeholder: '请输入主机端口',
                            label: '主机端口',
                            prop: 'port',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入用户名',
                            label: '用户名',
                            prop: 'username',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入密码',
                            label: '密码',
                            prop: 'password',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    //FTP
                    [
                        {
                            ltype: 'radio',
                            placeholder: '请选择连接模式',
                            label: '连接模式',
                            prop: 'mode',
                            width: 'w-1',
                            isShow: false,
                            options: getDictDatas('ftp_connect_type')
                        }
                    ],
                    //S3对象存储
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入节点地址',
                            label: '节点地址',
                            prop: 'endpoint',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入存储 Bucket',
                            label: '存储 Bucket',
                            prop: 'bucket',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入accessKey',
                            label: 'accessKey',
                            prop: 'accessKey',
                            width: 'w-1',
                            isShow: false
                        }
                    ],
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入accessSecret',
                            label: 'accessSecret',
                            prop: 'accessSecret',
                            width: 'w-1',
                            isShow: false
                        }
                    ]
                ],
                // 提交项
                formSubmit: {
                    mode: '1',
                    remark: ''
                },
                // 配置项
                formConfig: {
                    labelPosition: 'right', // label对齐方式
                    labelWidth: '120px'
                },
                // 验证项
                formRule: {
                    name: [
                        {
                            required: true,
                            message: '请输入配置名',
                            trigger: 'blur'
                        }
                    ],
                    storage: [
                        {
                            required: true,
                            message: '请选择存储器',
                            trigger: 'blur'
                        }
                    ],
                    domain: [
                        {
                            required: false,
                            message: '请输入自定义域名',
                            trigger: 'blur'
                        }
                    ],
                    basePath: [
                        {
                            required: false,
                            message: '请输入基础路径',
                            trigger: 'blur'
                        }
                    ],
                    host: [
                        {
                            required: false,
                            message: '请输入主机地址',
                            trigger: 'blur'
                        }
                    ],
                    port: [
                        {
                            required: false,
                            message: '请输入主机端口',
                            trigger: 'blur'
                        }
                    ],
                    username: [
                        {
                            required: false,
                            message: '请输入用户名',
                            trigger: 'blur'
                        }
                    ],
                    password: [
                        {
                            required: false,
                            message: '请输入密码',
                            trigger: 'blur'
                        }
                    ],
                    mode: [
                        {
                            required: false,
                            message: '请选择连接模式',
                            trigger: 'blur'
                        }
                    ],
                    endpoint: [
                        {
                            required: false,
                            message: '请输入节点地址',
                            trigger: 'blur'
                        }
                    ],
                    bucket: [
                        {
                            required: false,
                            message: '请输入存储 Bucket',
                            trigger: 'blur'
                        }
                    ],
                    accessKey: [
                        {
                            required: false,
                            message: '请输入accessKey',
                            trigger: 'blur'
                        }
                    ],
                    accessSecret: [
                        {
                            required: false,
                            message: '请输入accessSecret',
                            trigger: 'blur'
                        }
                    ]
                }
            }
        }
    },
    created() {
        this.initPage()
    },
    methods: {
        /**
         * 页面开始
         */
        async initPage() {
            // 查询岗位列表
            this.queryTableData(this.queryTableParams)
        },
        /**
         * 查询表格数据
         */
        async queryTableData(params) {
            this.baseTableConfig.tableConfig.loading = true
            let res = await listFileConfig(params)
            if (res.data) {
                this.baseTableConfig.tableData = res.data.list
                this.paginationConfig.total = res.data.totalCount
            } else {
                this.baseTableConfig.tableData = []
                this.paginationConfig.total = 0
            }
            this.baseTableConfig.tableConfig.loading = false
        },
        /**
         * 表格数据被选中
         */
        tableDataSelected(rows) {
            this.baseTableConfig.tableCheckedDatas = rows
        },
        /**
         * 初始化查询参数
         */
        initQueryParmas() {
            this.queryTableParams = {
                pageNo: 0,
                pageSize: 10,
                entity: {}
            }
            this.paginationConfig.size = 10
            this.paginationConfig.currentPage = 1
            this.baseTableConfig.tableData = []
        },

        /**
         * 页码变化
         */
        pageNumberChange(e) {
            this.queryTableParams.pageNo = e
            this.paginationConfig.currentPage = e
            this.queryTableData(this.queryTableParams)
        },
        /**
         * 页大小变化
         */
        pageSizeChange(e) {
            // 页大小变化时，将当前页码设置为0
            this.queryTableParams.pageNo = 0
            // 将当前表格数据设置为空
            this.baseTableConfig.tableData = []
            this.queryTableParams.pageSize = e
            this.queryTableData(this.queryTableParams)
        },

        /**
         * 搜索栏相关
         */
        /**
         * 搜索栏按钮点击
         * @param {Object} params
         */
        searchBarButtonClick(params) {
            // 清空查询参数
            this.initQueryParmas()
            try {
                this[params.prop]()
            } catch (error) {
                throw new Error(error)
            }
        },

        /**
         * 重置搜索表单
         */
        resetSearchForm() {
            // 查询参数初始化
            this.initQueryParmas()
            // 重置搜索表单
            this.$refs.searchForm.resetForm()
            // 查询数据
            this.refreshTableData()
        },

        /**
         * 搜索数据
         */
        searchDatas() {
            this.$refs.searchForm.submitForm(() => {
                // 清空查询参数
                this.initQueryParmas()
                // 组装查询条件
                this.queryTableParams.entity = {
                    ...this.searchFormConfig.formSubmit
                }
                // 查询数据
                this.queryTableData(this.queryTableParams)
            })
        },

        /**
         * 操作栏相关
         */
        /**
         * 操作项点击
         * @param {*} params
         */
        handelOptionClick(params) {
            this[params] ? this[params]() : null
        },

        /**
         * 切换搜索栏的显示状态
         */
        switchSearchStatus() {
            this.searchFormConfig.searchContenerShow = !this.searchFormConfig
                .searchContenerShow
        },

        /**
         * 刷新表格数据
         */
        refreshTableData() {
            // 将当前表格数据设置为空
            this.baseTableConfig.tableData = []
            this.queryTableData(this.queryTableParams)
        },

        /**
         * 设置表格显示隐藏列(打开弹框)
         */
        switchTableColumn() {
            let dialogParams = {
                title: '显示/隐藏',
                width: 'auto',
                show: true,
                key: 'tableColumn'
            }
            // 设置参与表格列控制的属性（最后的数组为不参与隐藏显示设置的列）
            this.tableColumnTransferConfig.data = assemblyDataByTableColumn(
                this.baseTableConfig.tableHeader,
                'label',
                ['选择', '岗位名称', '操作']
            )
            this.handelOpenDialog(dialogParams)
        },
        /**
         * 设置表格显示隐藏列(弹框确认)
         */
        switchTableColumnButtonClick() {
            let newData = this.$refs.baseTransfer.backCheckedData()
            if (newData && newData.length > 0) {
                this.baseTableConfig.tableHeader.forEach(ele => {
                    ele.visible = !newData.includes(ele.label)
                })
            } else {
                this.baseTableConfig.tableHeader.forEach(ele => {
                    ele.visible = true
                })
            }
            this.handelCloseDialog()
        },
        //处理提交参数
        dealSubmitParams(data) {
            var params = {
                id: data.id,
                name: data.name,
                storage: data.storage,
                remark: data.remark,
                config: {
                    ...data
                }
            }
            return params
        },

        /**
         * 新增表格数据(打开弹框)
         */
        addTableData() {
            // 打开新增弹框
            let dialogParams = {
                title: '新增',
                width: '50%',
                show: true,
                key: 'addTableData'
            }
            //默认对需要隐藏的列不进行展示
            this.showDataDeal([])

            //打开弹窗的
            this.handelOpenDialog(dialogParams)
            this.$nextTick(() => {
                this.initAddForm()
            })
        },

        /**
         * 新增数据确认提交(弹框确认)
         */
        addFormButtonClick() {
            this.$refs['addForm'].submitForm(async res => {
                if (res) {
                    let assRes = await saveFileConfig(
                        this.dealSubmitParams(res)
                    )
                    if (assRes && assRes.code === 0) {
                        this.$message.closeAll()
                        this.$message.success({ message: '新增成功！' })
                    } else {
                        this.$message.closeAll()
                        this.$message.error({ message: '新增失败！' })
                    }
                    this.handelCloseDialog()
                    // 查询数据
                    this.refreshTableData()
                    this.$nextTick(() => {
                        this.initAddForm()
                    })
                }
            })
        },
        /**
         * 初始化新增表单
         */
        initAddForm() {
            this.addFormConfig.formSubmit = {
                name: '',
                remark: '',
                storage: ''
            }
            this.$refs.addForm.resetForm()
        },
        /**
         * 修改表格数据(打开弹框)
         */
        async updateTableData(row) {
            row.storage = row.storage.toString()
            //控制即将要展示的菜单
            this.dealStorageShwo(row.storage)
            //对当前的存储器设置为只读，不让再进行修改
            this.updateFormConfig.formRule.storage[0].required = true
            this.updateFormConfig.formItems = setFormItemProp(
                this.updateFormConfig.formItems,
                'storage',
                'disabled',
                true
            )

            //先将当前表格的数据赋值
            var data = { ...row, ...row.config }
            this.updateFormConfig.formSubmit = data
            // 打开修改弹框
            let dialogParams = {
                title: '修改',
                width: '50%',
                show: true,
                key: 'updateTableData'
            }
            this.handelOpenDialog(dialogParams)
        },
        /**
         * 修改数据提交(弹框确认)
         */
        updateFormButtonClick() {
            this.$refs['updateForm'].submitForm(async res => {
                if (res) {
                    //先把res置空
                    res.config = {}
                    let assRes = await updateFileConfig(
                        this.dealSubmitParams(res)
                    )
                    if (assRes && assRes.code === 0) {
                        this.$message.closeAll()
                        this.$message.success({ message: '修改成功！' })
                        this.initUpdateForm()
                    } else {
                        this.$message.closeAll()
                        this.$message.error({ message: '修改失败！' })
                    }
                    this.handelCloseDialog()
                    // 查询数据
                    this.refreshTableData()
                }
            })
        },
        /**
         * 初始化修改表单
         */
        initUpdateForm() {
            this.updateFormConfig.formSubmit = {}
        },
        /**
         * 删除表格数据
         */
        deleteTableData(rows) {
            let params = {
                id: rows.id
            }
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    let res = await deleteFileConfig(params)
                    if (res && res.code == 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败!'
                        })
                    }
                    // 查询数据
                    this.refreshTableData()
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        setMasterTableData(rows) {
            let params = {
                id: rows.id
            }
            this.$confirm('是否将该数据修改为主配置?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    let res = await updateFileConfigMaster(params)
                    if (res && res.code == 0) {
                        this.$message({
                            type: 'success',
                            message: '设置成功!'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '设置失败!'
                        })
                    }
                    // 查询数据
                    this.refreshTableData()
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取设置'
                    })
                })
        },
        //测试文件是否上传
        async testFileConfigUpload(rows) {
            let params = {
                id: rows.id
            }
            let res = await testFileConfig(params)
            if (res && res.code == 0) {
                this.$message({
                    type: 'success',
                    message: '测试通过！'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: '测试失败!'
                })
            }
        },

        /**
         * 弹框相关
         */

        /**
         * 打开弹框
         * @param {*} params
         */
        handelOpenDialog(params) {
            this.dialogConfig = params
        },
        /**
         * 初始化弹框
         */
        handelInitDialog() {
            this.dialogConfig = {
                title: '',
                show: false,
                width: '',
                key: ''
            }
        },
        /**
         * 关闭弹框
         */
        handelCloseDialog() {
            this.handelInitDialog()
        },
        /**
         * 弹框确定
         */
        handelCofimeDialog() {
            // 表格列筛选的弹框
            if (this.dialogConfig.key === 'tableColumn') {
                this.switchTableColumnButtonClick()
            } else if (this.dialogConfig.key === 'addTableData') {
                this.addFormButtonClick()
            } else if (this.dialogConfig.key === 'updateTableData') {
                this.updateFormButtonClick()
            } else if (this.dialogConfig.key === 'permissionDataTableData') {
                this.permissionDataFormButtonClick()
            }
        },

        //数据权限中select的监听事件
        handerOnSelectChange(params) {
            if (params.prop === 'storage') {
                this.dealStorageShwo(params.data)
            }
        },
        //根据选中的类别去判断哪些要显示/隐藏
        dealStorageShwo(type) {
            var arrm
            if (type === '1') {
                arrm = ['domain']
            } else if (type === '2') {
                arrm = ['domain', 'basePath']
            } else if (type === '3') {
                arrm = [
                    'basePath',
                    'host',
                    'port',
                    'username',
                    'password',
                    'mode',
                    'domain'
                ]
            } else if (type === '4') {
                arrm = [
                    'basePath',
                    'host',
                    'port',
                    'username',
                    'password',
                    'domain'
                ]
            } else if (type === '5') {
                arrm = [
                    'endpoint',
                    'bucket',
                    'accessKey',
                    'accessSecret',
                    'domain'
                ]
            }
            this.dealRule(arrm, type)

            this.showDataDeal(arrm)
        },
        //修改新增、修改页面的表单项
        showDataDeal(params) {
            //此处是一直要保持显示的
            var alwayshow = ['name', 'remark', 'storage']
            this.addFormConfig.formItems.forEach(values => {
                values.forEach(val => {
                    if (alwayshow.indexOf(val.prop) == -1) {
                        if (params.indexOf(val.prop) > -1) {
                            this.setFormsItemsM(val.prop, 'isShow', true)
                        } else {
                            this.setFormsItemsM(val.prop, 'isShow', false)
                        }
                    }
                })
            })

            this.updateFormConfig.formItems.forEach(values => {
                values.forEach(val => {
                    if (alwayshow.indexOf(val.prop) == -1) {
                        if (params.indexOf(val.prop) > -1) {
                            this.setFormsItemsM(val.prop, 'isShow', true)
                        } else {
                            this.setFormsItemsM(val.prop, 'isShow', false)
                        }
                    }
                })
            })
        },

        //设置表单项
        setFormsItemsM(prop, labe, flag) {
            this.addFormConfig.formItems = setFormItemProp(
                this.addFormConfig.formItems,
                prop,
                labe,
                flag
            )
            this.updateFormConfig.formItems = setFormItemProp(
                this.updateFormConfig.formItems,
                prop,
                labe,
                flag
            )
        },

        //处理规则项,点击哪个，哪个的进行校验
        dealRule(arr, type) {
            //新增的
            Object.keys(this.addFormConfig.formRule).forEach(key => {
                if (arr.indexOf(key) > -1) {
                    if (type === '5' && key === 'domain') {
                        this.addFormConfig.formRule[key][0].required = false
                    } else {
                        this.addFormConfig.formRule[key][0].required = true
                    }
                } else {
                    this.addFormConfig.formRule[key][0].required = false
                }
            })

            this.addFormConfig.formRule.name[0].required = true
            this.addFormConfig.formRule.storage[0].required = true

            //修改的
            Object.keys(this.updateFormConfig.formRule).forEach(key => {
                if (arr.indexOf(key) > -1) {
                    if (type === '5' && key === 'domain') {
                        this.updateFormConfig.formRule[key][0].required = false
                    } else {
                        this.updateFormConfig.formRule[key][0].required = true
                    }
                } else {
                    this.updateFormConfig.formRule[key][0].required = false
                }
            })

            this.updateFormConfig.formRule.name[0].required = true
            this.updateFormConfig.formRule.storage[0].required = true
        }
    }
}
</script>
<style lang="scss" scoped></style>
